package cmd

import (
	"gitee.com/kristas/booting-go/gen_tool/booctl/logic"
	"gitee.com/kristas/booting-go/gen_tool/booctl/util"
	"github.com/spf13/cobra"
)

var (
	apiGroup string

	filterPattern string
	filterOrder   int

	repoTable      string
	repoDataSource string

	env string
)

var createCmd = &cobra.Command{
	Use:     "add [component]",
	Short:   "add component",
	Aliases: []string{"new", "create"},
}

var apiCmd = &cobra.Command{
	Use:     "controller [controller name]",
	Aliases: []string{"api", "ctr"},
	Short:   "Create a Controller Component",
	Run: func(cmd *cobra.Command, args []string) {
		if len(args) == 0 {
			util.Error("need a [controller name]")
		}
		if apiGroup == "" {
			cobra.CheckErr(util.Scan("input group prefix:", &apiGroup))
		}
		err := logic.CreateAPIHandler(args[0], apiGroup)
		cobra.CheckErr(err)
	},
}

var serviceCmd = &cobra.Command{
	Use:     "service [service name]",
	Aliases: []string{"svc"},
	Short:   "Create a Service Component",
	Run: func(cmd *cobra.Command, args []string) {
		if len(args) == 0 {
			util.Error("need a [service name]")
		}
		err := logic.CreateServiceHandler(args[0])
		cobra.CheckErr(err)
	},
}

var filterCmd = &cobra.Command{
	Use:     "filter [filter name]",
	Aliases: []string{"ftr"},
	Short:   "Create a Filter Component",
	Run: func(cmd *cobra.Command, args []string) {
		if len(args) == 0 {
			util.Error("need a [filter name]")
		}

		if filterPattern == "" {
			cobra.CheckErr(util.Scan("input filter pattern:", &filterPattern))
		}
		if filterOrder == 0 {
			cobra.CheckErr(util.Scan("input filter order:", &filterOrder))
		}

		err := logic.CreateFilterHandler(args[0], filterPattern, filterOrder)
		cobra.CheckErr(err)
	},
}

var repoCmd = &cobra.Command{
	Use:     "repository [repository name]",
	Aliases: []string{"repo"},
	Short:   "Create a Repository Component",
	Run: func(cmd *cobra.Command, args []string) {
		if len(args) == 0 {
			util.Error("need a [filter name]")
		}
		if repoTable == "" {
			cobra.CheckErr(util.Scan("input table name:", &repoTable))
		}
		err := logic.CreateRepositoryHandler(args[0], repoTable, repoDataSource)
		cobra.CheckErr(err)
	},
}

var configCmd = &cobra.Command{
	Use:     "config [config value]",
	Aliases: []string{"conf"},
	Short:   "Add to config",
	Run: func(cmd *cobra.Command, args []string) {
		if len(args) == 0 {
			util.Error("need a validate config")
		}
		if env == "" {
			cobra.CheckErr(util.Scan("input environment:", &env))
		}
		err := logic.CreateConfigHandler(env, args)
		cobra.CheckErr(err)
	},
}

func init() {
	apiCmd.Flags().StringVarP(&apiGroup, "group", "g", "", "used for create api group")

	filterCmd.Flags().StringVarP(&filterPattern, "pattern", "p", "", "used for filter mapping pattern")
	filterCmd.Flags().IntVarP(&filterOrder, "order", "o", 0, "used for filter chain order")

	repoCmd.Flags().StringVarP(&repoTable, "table", "t", "", "table name")
	repoCmd.Flags().StringVarP(&repoDataSource, "datasource", "d", "datasource", "datasource name")

	configCmd.Flags().StringVarP(&env, "environment", "e", "", "used for Specify the config file")

	createCmd.AddCommand(apiCmd)
	createCmd.AddCommand(serviceCmd)
	createCmd.AddCommand(filterCmd)
	createCmd.AddCommand(repoCmd)
	createCmd.AddCommand(configCmd)

	rootCmd.AddCommand(createCmd)
}
